import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		avatar: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user"))
			.avatar : "",
		username: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user"))
			.username : "",
		account: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user"))
			.account : "",
		isLogin: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user"))
			.isLogin : "",
		role: JSON.parse(sessionStorage.getItem("user")) ? JSON.parse(sessionStorage.getItem("user"))
			.role : -1,
	},
	getters: {
		getUserName: state => {
			return state.username
		},
		getUserAccount: state => {
			return state.account
		},
		getUserAvatar: state => {
			return state.avatar
		},
		isshowAdmin: state => {
			if (state.role == 2) {
				return false
			}
			return true
		},
		getIsSuper: state => {
			if (state.role == 0) {
				return true
			}
			return false
		},
		getLogin: state => {
			return state.isLogin
		}
	},
	mutations: {
		setUserName(state, name) {
			state.username = name
			sessionStorage.setItem("user", JSON.stringify({
				avatar: state.avatar,
				username: name,
				account: state.account,
				role: state.role,
				isLogin: true
			}))
		},
		setUserAvatar(state, avatar) {
			state.avatar = avatar
			sessionStorage.setItem("user", JSON.stringify({
				avatar: avatar,
				username: state.username,
				account: state.account,
				role: state.role,
				isLogin: true
			}))
		},
		setUser(state, data) {
			state.avatar = data.admin_avatar
			state.username = data.admin_name
			state.account = data.admin_account
			state.role = data.admin_role
			state.isLogin = true
			sessionStorage.setItem("user", JSON.stringify({
				avatar: data.admin_avatar,
				username: data.admin_name,
				account: data.admin_account,
				role: data.admin_role,
				isLogin: true
			}))
		},
		logout(state) {
			state.avatar = ""
			state.username = ""
			state.account = ""
			state.isLogin = false
			sessionStorage.removeItem("user")
		}
	}
})

export default store